package com.yk.filedownload.threadpool;

import com.yk.filedownload.ConfigIt.Configuration;
import com.yk.filedownload.downloader.DownloadCell.CommonCell;

import java.util.concurrent.*;

/**
 * Project Name:cykfiledownload
 * File Name:DownloadThreadPool
 * Package Name:com.yk.filedownload.manager.ThreadPool
 * Date:2017\12\11 0011
 * Author:dengtianjia
 * Description:
 * Copyright (c) 2017, 重庆云凯科技有限公司 All Rights Reserved.
 */
public class DownloadThreadPool extends ThreadPoolExecutor{


    public  LinkedBlockingQueue<CommonCell> workQueue = new LinkedBlockingQueue<CommonCell>(Configuration.CORE_POOL_SIZE);
    /**
     *
     * @param corePoolSize  核心池大小
     * @param maximumPoolSize 最大线程数
     * @param keepAliveTime 空闲线程最大保留时间
     */
    public DownloadThreadPool(int corePoolSize, int maximumPoolSize, long keepAliveTime){
        super(corePoolSize,maximumPoolSize,keepAliveTime,TimeUnit.MILLISECONDS,new LinkedBlockingDeque<Runnable>());
    }

    @Override
    protected void beforeExecute(Thread t, Runnable r) {
        super.beforeExecute(t, r);
        if(CommonCell.class.isAssignableFrom(r.getClass())) {
            workQueue.add((CommonCell) r);
        }

    }


    @Override
    protected void afterExecute(Runnable r, Throwable t) {
        super.afterExecute(r, t);
        if(CommonCell.class.isAssignableFrom(r.getClass())) {
            workQueue.remove(r);
        }

    }

    public  LinkedBlockingQueue<CommonCell> getWorkQueue() {
        return workQueue;
    }
}
